package com.vancouver.knows.ui.fragment.event;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.ListItemBrowser;
import com.pangu.ui.browser.PullListItemBrowser;
import com.pangu.ui.refresh.ARefreshViewController;
import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.MainApplication.TrackerName;
import com.vancouver.knows.R;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.EventInfo;
import com.vancouver.knows.ui.activity.event.EventDetailActivity;
import com.vancouver.knows.util.AnalyticsConstants;
import com.vancouver.knows.util.AppUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class EventCalendarListView extends ListItemBrowser<MainApplication> {

	private BusinessModule bModule;
	private List<EventInfo> mItems;

	public EventCalendarListView(Context context) {
		super(context);
		init();
	}

	public EventCalendarListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public void setItems(List<EventInfo> mItems) {
		this.mItems = mItems;
		BaseAdapter baseAdapter = getMAdapter();
		if(baseAdapter!=null){
			baseAdapter.notifyDataSetChanged();
		}
	}

	private void init() {
		bModule = imContext.getBusinessModule();
	}

	@Override
	protected BaseAdapter createAdapter() {
		return new EventAdapter();
	}

	@Override
	protected void handleLoadNewItems(ATaskMark taskMark) {
	}

	@Override
	protected void setListViewParameters(ListView listView) {
		super.setListViewParameters(listView);
		listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
		listView.setDividerHeight(1);
	}

	public class EventAdapter extends BaseAdapter{

		private LayoutInflater inflater;

		public EventAdapter() {
			inflater = LayoutInflater.from(getContext());
		}

		private List<EventInfo> getDatas(){
			return mItems;
		}

		@Override
		public int getCount() {
			return  getDatas()==null? 0: getDatas().size();
		}

		@Override
		public EventInfo getItem(int position) {
			return getDatas().get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = inflater.inflate(R.layout.event_calendar_item, parent, false);
				ChildViewHolder viewHolder = new ChildViewHolder(convertView);
				convertView.setTag(viewHolder);
			}

			EventInfo eventInfo = getItem(position);
			if (eventInfo != null) {
				initItemView(convertView, eventInfo, position);
			}

			return convertView;
		}

		private void initItemView(View convertView, final EventInfo eventInfo, final int position) {
			ChildViewHolder viewHolder = (ChildViewHolder) convertView.getTag();
			viewHolder.title.setText(eventInfo.getTitle());
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getContext(), EventDetailActivity.class);
					intent.putExtra("postId", eventInfo.getId());
					getContext().startActivity(intent);
				}
			});
		}

		public class ChildViewHolder {

			@Bind(R.id.title)
			TextView title;

			public ChildViewHolder(View view) {
				ButterKnife.bind(this, view);
			}

		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

}
